//
//  YWAddModelNSObject.m
//
//  Created by Yeewii@hotmail com on 16/6/15
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "YWAddModelNSObject.h"
#import "YWAddModelImgs.h"


NSString *const kYWAddModelNSObjectMd5 = @"md5";
NSString *const kYWAddModelNSObjectImgs = @"imgs";


@interface YWAddModelNSObject ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation YWAddModelNSObject

@synthesize md5 = _md5;
@synthesize imgs = _imgs;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.md5 = [self objectOrNilForKey:kYWAddModelNSObjectMd5 fromDictionary:dict];
    NSObject *receivedYWAddModelImgs = [dict objectForKey:kYWAddModelNSObjectImgs];
    NSMutableArray *parsedYWAddModelImgs = [NSMutableArray array];
    if ([receivedYWAddModelImgs isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedYWAddModelImgs) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedYWAddModelImgs addObject:[YWAddModelImgs modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedYWAddModelImgs isKindOfClass:[NSDictionary class]]) {
       [parsedYWAddModelImgs addObject:[YWAddModelImgs modelObjectWithDictionary:(NSDictionary *)receivedYWAddModelImgs]];
    }

    self.imgs = [NSArray arrayWithArray:parsedYWAddModelImgs];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.md5 forKey:kYWAddModelNSObjectMd5];
    NSMutableArray *tempArrayForImgs = [NSMutableArray array];
    for (NSObject *subArrayObject in self.imgs) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForImgs addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForImgs addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForImgs] forKey:kYWAddModelNSObjectImgs];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.md5 = [aDecoder decodeObjectForKey:kYWAddModelNSObjectMd5];
    self.imgs = [aDecoder decodeObjectForKey:kYWAddModelNSObjectImgs];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_md5 forKey:kYWAddModelNSObjectMd5];
    [aCoder encodeObject:_imgs forKey:kYWAddModelNSObjectImgs];
}

- (id)copyWithZone:(NSZone *)zone
{
    YWAddModelNSObject *copy = [[YWAddModelNSObject alloc] init];
    
    if (copy) {

        copy.md5 = [self.md5 copyWithZone:zone];
        copy.imgs = [self.imgs copyWithZone:zone];
    }
    
    return copy;
}


@end
